/**
 * 货物仓位数据
 */


// type=1货架，2过道中间，3过道长，4牌子
function getList(){
    var formData = {};
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'shelf/index',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg});
                return
            }
            getHtml(res.data);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//字符串
function getHtml(d){
    var h = '', i, ii, rowList, type;
    h += '<table>';
    for (i = 0; i < d.length; i ++) {
        h += '<tr>';
        rowList = d[i].rowList;
        for (ii = 0; ii < rowList.length; ii ++) {
            type = rowList[ii].type;
            if (type == 2) {
                h += '<td class="guodao">';
            } else if (type == 3) {
                h += '<td class="paizi">';
            } else {
                h += '<td class="huojia" id="hj' + rowList[ii].id + '" title="货架信息' + rowList[ii].id + '">';
            }
            if (type > 1 || rowList[ii].title == '') {
                h += rowList[ii].type_name;
            }
            h += rowList[ii].title;
            h += '</td>';
        }
        h += '</tr>';
    }
    h += '</table>';
    $('#shelfList').html(h);
}


// 获取货物号列表
function hwList(){
    var code = $('#hw_kw').val();
    var formData = {
        hw_kw: code,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'hwss/hwbykey',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg});
                return
            }
            var d = res.data;
            var h = '', i;
            for (i = 0; i < d.length; i ++) {
                h += '<option value="' + d[i].id + '">';
                h += d[i].title;
                h += '</option>';
            }
            $('#hw_id').html(h);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


// 找仓位
function emptyCw(){
    var hw_id = $('#hw_id').val();
    var num = $('#hw_num').val();
    var formData = {
        hw_id: hw_id, num: num,
    };
    // console.log(formData); return
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'hwss/cwList',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                $('.huojia').removeClass('hjed');
                $('#cwList').html('');
                msgPop({msg: res.msg});
                return
            }
            var d = res.data.data;
            var h = '';
            h += '<style type="text/css">.cwGroup { margin-bottom: 5px; padding: 10px 0;width: 100%; font-size: 14px; color: #555; } .cwTd { padding: 5px 20px; } </style>';
            for (i = 0; i < d.length; i ++) {
                h += '<div class="cwGroup">';
                h += '<div class="cwTd">仓位号：' + d[i].cw_name + '</div>';
                h += '<div class="cwTd">货物号：' + d[i].hw_code + '</div>';
                h += '<div class="cwTd">货物名称：' + d[i].hw_name + '</div>';
                h += '<div class="cwTd">库存：' + d[i].stock + '</div>';
                h += '<div class="cwTd">空位：' + d[i].hw_num + '</div>';
                h += '</div>';
            }
            $('#cwList').html(h);
            var shelfList = res.data.shelf;
            for (i = 0; i < shelfList.length; i ++) {
                $('.huojia').removeClass('hjed');
                $('#hj' + shelfList[i]).addClass('hjed');
            }
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


// 找货物
function houwu(){
    var hw_id = $('#hw_id').val();
    var num = $('#hw_num').val();
    var formData = {
        hw_id: hw_id, num: num,
    };
    // console.log(formData); return
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'hwss/hwList',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                $('.huojia').removeClass('hjed');
                $('#cwList').html('');
                msgPop({msg: res.msg});
                return
            }
            var d = res.data.data;
            var h = '';
            h += '<style type="text/css">.cwGroup { margin-bottom: 5px; padding: 10px 0; width: 100%; font-size: 14px; color: #555; } .cwTd { padding: 5px 20px; } </style>';
            for (i = 0; i < d.length; i ++) {
                h += '<div class="cwGroup">';
                h += '<div class="cwTd">仓位号：' + d[i].cw_name + '</div>';
                h += '<div class="cwTd">货物号：' + d[i].hw_code + '</div>';
                h += '<div class="cwTd">货物名称：' + d[i].hw_name + '</div>';
                h += '<div class="cwTd">库存：' + d[i].stock + '</div>';
                h += '</div>';
            }
            $('#cwList').html(h);
            var shelfList = res.data.shelf;
            for (i = 0; i < shelfList.length; i ++) {
                $('.huojia').removeClass('hjed');
                $('#hj' + shelfList[i]).addClass('hjed');
            }
        },
        error: function () {
            console.log('网络错误');
        }
    });
}